<template>
  <d2-container>
    <template slot="header">
      <i class="el-icon-s-order"></i>&nbsp;切片列表
    </template>
    <div>
      <el-button type="success" @click="gotoSetting">切片设置</el-button>
      <el-button type="primary" @click="gotoBounder">绑定切片</el-button>
      <el-button type="info" @click="getLists">获取切片列表</el-button>
      <el-button type="info" @click="auto">自适应最佳切片模板</el-button>
    </div>
    <div>
      <el-divider>
        <i class="el-icon-s-order"></i>UL切片列表
      </el-divider>
      <el-table ref="filterTable" :data="ULSlicingLists" style="width: 100%">
        <el-table-column prop="bs_id" label="bs_id" sortable width="180" column-key="id"></el-table-column>
        <el-table-column
          prop="algorithm"
          label="算法"
          width="100"
          :filters="[
            { text: 'static', value: 'static' },
            { text: 'none', value: 'none' },
          ]"
          filter-placement="bottom-end"
        >
          >
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.algorithm === 'static' ? 'success' : 'warning'"
              disable-transitions
            >{{ scope.row.algorithm }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="ULSlicingId" label="UL切片id" sortable width="180" column-key="id"></el-table-column>
        <el-table-column prop="posLow" label="posLow"></el-table-column>
        <el-table-column prop="posHigh" label="posHigh"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-divider>
        <i class="el-icon-s-order"></i>DL切片列表
      </el-divider>
      <el-table ref="filterTable" :data="DLSlicingLists" style="width: 100%">
        <el-table-column prop="bs_id" label="bs_id" sortable width="180" column-key="id"></el-table-column>
        <el-table-column
          prop="algorithm"
          label="算法"
          width="100"
          :filters="[
            { text: 'static', value: 'static' },
            { text: 'none', value: 'none' },
          ]"
          filter-placement="bottom-end"
        >
          >
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.algorithm === 'static' ? 'success' : 'warning'"
              disable-transitions
            >{{ scope.row.algorithm }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="DLSlicingId" label="DL切片id" width="180" sortable column-key="id"></el-table-column>
        <el-table-column prop="posLow" label="posLow"></el-table-column>
        <el-table-column prop="posHigh" label="posHigh"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </d2-container>
</template>
<script>
import Axios from 'axios'
export default {
  data () {
    return {
      ULSlicingLists: [],
      DLSlicingLists: [],
      SlicingAlias:{
        '0': '预留切片',
        '1': '课题一切片',
        '2': '课题二切片',
        '3': '课题三切片',
      },
      slice_configuration: {
        ul: {
          algorithm: 'Static',
          slices: [
            {
              id: 1,
              static: {
                posLow: 25,
                posHigh: 48
              }
            },
          ]
        },
        dl: {
          algorithm: 'Static',
          slices: [
            {
              id: 1,
              static: {
                posLow: 13,
                posHigh: 24
              }
            },
          ]
        }
      }
    }
  },
  methods: {
    auto () {
      Axios({
        method: 'post',
        url: '/api/slice/enb/-1',
        data: this.slice_configuration
      }).then(res => {
        console.log(res.data)
        setTimeout(() => {
          this.getLists()
        }, 1000)
      })
    },
    handleDelete (index, row) {
      let data = {}
      if (row.DLSlicingId != undefined) {
        data = {
          dl: {
            slices: [
              {
                id: row.DLSlicingId
              }
            ]
          }
        }
      }
      if (row.ULSlicingId != undefined) {
        data = {
          ul: {
            slices: [
              {
                id: row.ULSlicingId
              }
            ]
          }
        }
      }
      console.log(data)
      Axios.delete('/api/slice/enb/' + row.bs_id, {
        data
      }).then(res => {
        console.log(res)
        setTimeout(() => {
          this.getLists()
        }, 1000)
      })
    },
    gotoSetting () {
      this.$router.push({ path: '/slicing' })
    },
    gotoBounder () {
      this.$router.push({ path: '/slicingBounder' })
    },
    getLists () {
      Axios({
        method: 'get',
        url: '/api/stats'
      }).then(res => {
        this.ULSlicingLists = []
        this.DLSlicingLists = []
        const enbLists = res.data.eNB_config
        for (let i = 0; i < enbLists.length; ++i) {
          const bs_id = enbLists[i].bs_id
          const dl = enbLists[i].eNB.cellConfig[0].sliceConfig.dl
          const ul = enbLists[i].eNB.cellConfig[0].sliceConfig.ul
          if (dl.algorithm === 'None') {
            this.DLSlicingLists.push({
              bs_id,
              algorithm: 'none'
            })
          } else {
            const slices = dl.slices
            for (let j = 0; j < slices.length; ++j) {
              this.DLSlicingLists.push({
                bs_id,
                algorithm: 'static',
                DLSlicingId: this.SlicingAlias[slices[j].id],
                posLow: slices[j].static.posLow,
                posHigh: slices[j].static.posHigh
              })
            }
          }

          if (ul.algorithm === 'None') {
            this.ULSlicingLists.push({
              bs_id,
              algorithm: 'none'
            })
          } else {
            const slices = ul.slices
            for (let j = 0; j < slices.length; ++j) {
              this.ULSlicingLists.push({
                bs_id,
                algorithm: 'static',
                ULSlicingId: this.SlicingAlias[slices[j].id],
                posLow: slices[j].static.posLow,
                posHigh: slices[j].static.posHigh
              })
            }
          }
        }
        console.log(this.ULSlicingLists)
        console.log(this.DLSlicingLists)
      })
    }
  },
  //   mounted() {
  //     this.getLists();
  //   },
  created () {
    this.getLists()
  }
}
</script>